package com.itheima.d01_threadpool_runnable;

import java.util.List;
import java.util.concurrent.*;

/**
 * 目标：掌握线程池执行Runnable任务
 */
public class Test {

    public static void main(String[] args) {
        //1.创建线程池对象
        ExecutorService pool = new ThreadPoolExecutor(
                2, //核心线程数
                5, //最大线程数
                3, //线程空闲时间
                TimeUnit.SECONDS, //时间单位
                new ArrayBlockingQueue<>(3), //任务队列
                Executors.defaultThreadFactory(), //线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() //直接拒绝
        );

        //2.执行任务
        pool.execute(()->{
            System.out.println("顾客1：吃饭...，"+Thread.currentThread().getName()+"：服务");

        });

/*
        pool.execute(()->{
            System.out.println("顾客2：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });


        pool.execute(()->{
            System.out.println("顾客3：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });


        pool.execute(()->{
            System.out.println("顾客4：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });


        pool.execute(()->{
            System.out.println("顾客5：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        pool.execute(()->{
            System.out.println("顾客6：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        pool.execute(()->{
            System.out.println("顾客7：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        pool.execute(()->{
            System.out.println("顾客8：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        pool.execute(()->{
            System.out.println("顾客9：吃饭...，"+Thread.currentThread().getName()+"：服务");
            try {
                Thread.sleep(5000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });*/


        //注意：一般不建议关闭线程池
        //pool.shutdown();//等待所有任务执行完毕，再关闭线程池
        //List<Runnable> runnables = pool.shutdownNow();//暴力关闭线程池
        //System.out.println("暴力关闭未执行的任务："+runnables);
        System.out.println("主线程执行over");

    }
}
